//
//  Extension+UITableView.swift
//  swift-test
//
//  Created by 梁森 on 2021/4/14.
//  Copyright © 2021 apple. All rights reserved.
//

import Foundation
import UIKit

// cell可遵守此协议
public protocol RegisterRepeat { }

public extension RegisterRepeat {
	static var identifier: String {
		return "\(self)"
	}
	static var xib: UINib? {
		return nil
	}
}

public struct Prefix<T> {
	 let base: T
	 init(_ base: T) {
		self.base = base
	}
}

// tableview可遵守此协议
public protocol PrefixProtocol {
	associatedtype CompatibleType
	var ls: Prefix<CompatibleType> { get set }
}

public extension PrefixProtocol {
	var ls: Prefix<Self> {
		get { return Prefix(self) }
		set {}
	}
}

extension UITableView: PrefixProtocol { }

extension Prefix where T == UITableView {
	public func registerCell<A: UITableViewCell>(_ cell: A.Type) where A: RegisterRepeat {
		if let xib = A.xib {
			base.register(xib, forCellReuseIdentifier: A.identifier)
		} else {
			base.register(cell, forCellReuseIdentifier: A.identifier)
		}
	}
	
	public func dequeueReusableCell<A: UITableViewCell>(indexPath: IndexPath) -> A where A: RegisterRepeat {
		return base.dequeueReusableCell(withIdentifier: A.identifier, for: indexPath) as! A
	}
}



